package cn.topcodes.tcsf.admin.web.controller.impl;

import cn.topcode.unicorn.utils.IpUtil;
import cn.topcode.unicorn.utils.ValidationUtil;
import cn.topcodes.tcsf.admin.commons.api.controller.admin.AdminController;
import cn.topcodes.tcsf.admin.commons.domain.ValueRangeConstant;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.domain.entity.SysDept;
import cn.topcodes.tcsf.admin.domain.entity.SysRole;
import cn.topcodes.tcsf.admin.domain.entity.SysUser;
import cn.topcodes.tcsf.admin.domain.form.CreateSysUserForm;
import cn.topcodes.tcsf.admin.domain.form.UpdateSysUserForm;
import cn.topcodes.tcsf.admin.service.org.SysDeptService;
import cn.topcodes.tcsf.admin.service.org.SysRoleService;
import cn.topcodes.tcsf.admin.service.org.SysUserService;
import cn.topcodes.tcsf.admin.web.controller.SysUserApi;
import cn.topcodes.tcsf.admin.commons.api.security.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 系统用户管理控制器
 * @author Unicorn
 */
@Controller
@RequestMapping("/admin/sys-user")
public class SysUserAdminController extends AdminController<SysUser, SysUser, UpdateSysUserForm> implements SysUserApi {

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysDeptService sysDeptService;

	@Autowired
	private SysUserService sysUserService;

	@Override
	protected String getPageFolder() {
		return "admin/user";
	}

	@RequestMapping(value = "quick-create",method = RequestMethod.GET)
	public String quickCreate(HttpServletRequest request, Model model) {
		List<SysRole> roles = this.sysRoleService.select(ShiroUtil.getUserId());
		model.addAttribute("roles",roles);
		return super.create(request, model);
	}

	/*根据名称查询列表*/
	@ResponseBody
	@RequestMapping(value = "list-by-name",method = RequestMethod.GET)
	public ApiResponseEntity findAll(String name,HttpServletRequest request) {
		List<SysUser> list = this.sysUserService.findAll(ShiroUtil.getUserId(), name);
		List<SysUser> vos = this.entity2Vo(list,request);
		return ApiResponseEntity.buildOK().setBody(vos);
	}

	@Override
	public String update(Long id, HttpServletRequest request, Model model) {
		List<SysRole> roles = this.sysRoleService.select(ShiroUtil.getUserId());
		model.addAttribute("roles",roles);
		List<SysDept> depts = this.sysDeptService.findAllEnable();
		model.addAttribute("depts",depts);
		SysRole role = this.sysUserService.getRole(id);
		model.addAttribute("roleId",role.getId());
		return super.update(id, request, model);
	}

	@ResponseBody
	@RequestMapping(value = "quick-create",method = RequestMethod.POST)
	public ApiResponseEntity quickCreate(
			HttpServletRequest request,
			@Valid CreateSysUserForm form,
			BindingResult result) {
		ValidationUtil.validate(result);
		SysUser user = new SysUser();
		user.setUsername(form.getUsername());
		user.setPassword(form.getPassword());
		user.setGender(ValueRangeConstant.Gender.GENDER_UNKNOWN);
		user.setRealName(form.getRealName());
		String ip = IpUtil.getIp(request);
		this.sysUserService.register(user,form.getRoleId(), ip);
		return ApiResponseEntity.buildOK();
	}

	@Override
	public ApiResponseEntity<Void> update(
			HttpServletRequest request,
			@Valid UpdateSysUserForm form, BindingResult result) {
		ValidationUtil.validate(result);
		this.sysUserService.update(form);
		return ApiResponseEntity.buildOK();
	}

	/*重置密码页面*/
	@RequestMapping(value="reset-password",method=RequestMethod.GET)
	public String resetPassword(@RequestParam Long id, Model model) {
		model.addAttribute("id",id);
		return "admin/user/reset-password";
	}

	/*重置密码*/
	@ResponseBody
	@RequestMapping(value = "reset-password",method = RequestMethod.POST)
	public ApiResponseEntity resetPassword(
			@RequestParam Long id,
			@RequestParam String newPassword,
			@RequestParam String adminPassword
	) {
		boolean isPwdEqual = this.validateOperation(adminPassword);
		if(!isPwdEqual) {
			return ApiResponseEntity.buildIllegalArgument("管理密码输入错误");
		}
		this.sysUserService.resetPassword(id,newPassword);
		return ApiResponseEntity.buildOK();
	}

}